/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author kpatel
 */
/*
 * This will model RAM for the architecture.
 * This class will create a RAM and load data from a text file to RAM.
 *
 *
 */
//import java.lang.reflect.InvocationTargetException;
//import javax.swing.JOptionPane;
import java.io.*;


public class Ram
{

    public int input_data_start,input_data_end ;
    public int input_coefficient_start,input_coefficient_end;
    public int[] ram;
    
    public int no_of_samples;
    
    String data_line = null;
    
    //File handler for input data file
    public  File in_data_file;
    String in_file_name = "";
    
    //File handler for output data file
    public static File out_data_file;
    String out_file_name = "Output.txt";
    public static boolean write_output_flag =false;
    
    int data_line_int;
	int ram_array_index=0;
	public static int RAM_accesses = 0;
	
	// This is constructor of class Ram. We will fill RAM by reading a text.

    public void Load_Coefficients()
    {

        try
		{

			BufferedReader in_data_buffer = new BufferedReader(new FileReader(in_file_name));
        	//BufferedReader in_data_buffer = new BufferedReader(new FileReader());

			while((data_line = in_data_buffer.readLine()) != null)
			{
				//System.out.println("The input string is " + data_line);
                input_coefficient_start = data_line.compareTo("filter_coefficients_start");

                //System.out.println(input_coefficient_start);
                if(input_coefficient_start==0)
                {
                    while ((data_line = in_data_buffer.readLine()) != null)
                    {
                    //System.out.println("The coefficient is " + data_line);
                    input_coefficient_end = data_line.compareTo("filter_coefficients_end");
                    	if (input_coefficient_end !=0)
                    		{
			                    data_line_int = Integer.parseInt(data_line);
			                    //System.out.println(data_line_int);
			                    ram[ram_array_index] = data_line_int;
			                    ram_array_index ++;
                    		}
                    	else break;
                    }
                }

			}
            //JOptionPane.showMessageDialog(null,"The coefficients are loaded");
//			for(int i=0;i<ram_array_index;i++)
//			{
//				//System.out.print("i="+i+"\n" );
//				//System.out.print("The input to ram is " + ram[i]+ "\n");
//			System.out.print("ram ["+i+"] = " + ram[i]+ "\n");
//
//			}


		}
        catch (FileNotFoundException e)
			{
				e.printStackTrace();
				System.out.println("The input file for RAM is not found!");

			}catch (IOException e)
			{
				e.printStackTrace();

            }
    }

        public void Load_Data()
    {
        	int total_samples=0;
        	//System.out.println("loading data now");
        try
		{
			BufferedReader in_data_buffer = new BufferedReader(new FileReader(in_file_name));


			while((data_line = in_data_buffer.readLine()) != null && no_of_samples > total_samples)
                
			{
				//System.out.println("The input string is " + data_line);
                input_data_start = data_line.compareTo("input_data_start");
                //System.out.println(input_data_start);


                if(input_data_start==0)
                {
                	//Check end of file or total number of samples user wants to process
                    while ((data_line = in_data_buffer.readLine()) != null && no_of_samples > total_samples)
                    {
                    //System.out.println("The data is " + data_line);
                    input_data_end = data_line.compareTo("input_data_end");
                    total_samples ++;
                    if(input_data_end!=0)
		                  {
		                    data_line_int = Integer.parseInt(data_line);
		                    //System.out.println(data_line_int);
		                    ram[ram_array_index] = data_line_int;
		                    ram_array_index ++;
		                  }
                    else break;
                    }
                }
                
			}
 

		}catch (FileNotFoundException e)
			{
				e.printStackTrace();
				System.out.println("The input file for RAM is not found!");

			}catch (IOException e)
			{
				e.printStackTrace();

            }


    }
        
        public static void Write_Output(long out_data)
        {
        	
        	try {
                // Create a FileWriter stream to the file
                FileWriter file_writer = new FileWriter (out_data_file);

                // Put a buffered wrappter around it
                BufferedWriter buf_writer = new BufferedWriter (file_writer);

                // In turn, wrap the PrintWriter stream around this output stream
                // and turn on the autoflush.
                PrintWriter print_writer = new PrintWriter (buf_writer,true);

                // Use the PrintWriter println methods to send strings and
                // primitives to the file.
//                print_writer.println ("Text output with PrintWriter.");
//                print_writer.println ("Primitives converted to strings:");
              //print_writer.write(Long.toString(out_data));
                //print_writer.append(Long.toString(out_data));
                //buf_writer.
                
                // PrintWriter doesn't throw IOExceptions but instead
                // offers the catchError() method
                if (print_writer.checkError ()) {
                    System.out.println ("An output error occurred!" );
                }

            }
            catch (IOException ioe){
              System.out.println("IO Exception");
            }
        	
        	
        	
        }

	public Ram(int ram_size, int samples_entered)
    {

		in_data_file  = new File (in_file_name);
		
		out_data_file = new File (out_file_name);
		
		//StringBuffer contents = new StringBuffer();
		data_line = null;
		no_of_samples = samples_entered;
        input_data_start = 1;input_data_end = 1;
        input_coefficient_start = 1;input_coefficient_end = 1;
        ram = new int [ram_size];
        RAM_accesses = 0;
        
        write_output_flag = false;
        
    }

}